CC = riscv64-unknown-elf-gcc
LD = riscv64-unknown-elf-ld
OBJCOPY = riscv64-unknown-elf-objcopy
QEMU = qemu-system-riscv64

# 关键修改：添加 -mcmodel=medany 支持64位任意地址
CFLAGS = -nostdlib -fno-builtin -fno-builtin-va-list -mno-relax -Wall -Werror -Iinclude -c -march=rv64gc -mabi=lp64 -mcmodel=medany
LDFLAGS = -nostdlib -T kernel/kernel.ld -m elf64lriscv -L/usr/lib/gcc/riscv64-unknown-elf/10.2.0/rv64i/lp64 -lgcc

OBJS = kernel/entry.o kernel/uart.o kernel/console.o kernel/printf.o kernel/spinlock.o kernel/main.o

all: kernel.bin

kernel.bin: $(OBJS)
	$(LD) $(LDFLAGS) -o kernel.elf $(OBJS)
	$(OBJCOPY) -O binary kernel.elf kernel.bin

%.o: %.c
	$(CC) $(CFLAGS) -o $@ $<

%.o: %.S
	$(CC) $(CFLAGS) -o $@ $<

run: kernel.bin
	qemu-system-riscv64 -machine virt -kernel kernel.bin -nographic
clean:
	rm -f $(OBJS) kernel.elf kernel.bin
.PHONY: all run clean
